$(document).ready(function(){
        
    // init setting for timeago
    jQuery.timeago.settings.strings = {
        prefixAgo: null,
        prefixFromNow: null,
        suffixFromNow: "from now",
        seconds: "less than a minute",
        minute: "1m",
        minutes: "%dm",
        hour: "1h",
        hours: "%dh",
        day: "1d",
        days: "%dd",
        month: "1 month",
        months: "%dm",
        year: "1y",
        years: "%dy",
        wordSeparator: " ",
        numbers: []
    };
	
        
    // Nav control
    var open = false;
    $("#user-dropdown-toggle").click(function(){
        if (!open)
            $("#user-dropdown").addClass("open");
        else
            $("#user-dropdown").removeClass("open");
        open = !open;
        return false;
    });
        
    // Post tweet button
    $("#global-new-tweet-button").click(function(){
        var postView = new PostView({
            });
        bootbox.modal(postView.render().$el, "What's happening?");
        $(".post-content").focus();
        $(".post-content").val("");
    });
    PostView = Backbone.View.extend({
        template : $("#postTemplate").html(),
        
        events : {
            'click .tweet-button'   : "postTweet",
            'keyup .post-content'   : "contentKeyup",
            'keydown .post-content' : "contentKeydown"
        },
        initialize : function(){
        },
        render : function(){
            var tmpl = _.template(this.template);
            if (this.model != null)
                this.$el.html(tmpl(this.model.toJSON()));
            else
                this.$el.html(tmpl({"parent_id" : "0"}));
            return this;
        },
        
        postTweet : function(e) {
            e.preventDefault();
            
            var content = this.$el.find(".post-content").val();
            if ((content.length == 0) || (content.length == 140))
                return;
            
            var data = { content: content };
            var url  = TWEET_API_URL + 'post';
            if (this.model != null){
                data.tweet_id = this.model.get("tweet_id");
                url = TWEET_API_URL + 'reply'
            }
            $.ajax({
                type   : "POST",
                url    : url,
                data   : data
            }).done(function() { 
                masterView.getNew();
            });
                
            bootbox.hideAll();
        },
        
        contentKeyup : function(e){
            e.preventDefault();
            var contentElm = this.$el.find(".post-content");
            var counterElm = this.$el.find(".tweet-counter");
            counterElm.val(140 - contentElm.val().length);
            if ((contentElm.val().length < 140) && (contentElm.val().length > 0))
                this.$el.find(".tweet-button").removeClass("disabled");
            else if (!this.$el.find(".tweet-button").hasClass("disabled"))
                this.$el.find(".tweet-button").addClass("disabled");
        },
        
        contentKeydown : function(e){
            if (e.keyCode == 8) return;
            var counterElm = this.$el.find(".tweet-counter");
            if (counterElm.val() + 0  <= 0 ) {
                e.preventDefault();
                return;
            }
        }
    });
});